草庐IT

c++ - GenericFactory 作为单例

全部标签

sql - 如何在 SQL 查询中将 URI 作为字符串附加

看起来很简单,但我做不到。当浏览domain.com/post/1时,它应该显示id行的数据,其值为1。行id是整数(int4)。下面的代码,这是行不通的:packagemainimport"fmt"import"github.com/go-martini/martini"import"net/http"import"database/sql"import_"github.com/lib/pq"funcSetupDB()*sql.DB{db,err:=sql.Open("postgres","user=postgrespassword=apassworddbname=lesson4ss

json - 如何在 REST 端点中将结构作为有效 JSON 返回?

我正在从API获取一些数据,我想在我的Go应用程序的REST端点中提供这些数据。结构是这样的:typeStockstruct{Stockstring`json:"message_id,omitempty"`StockDatamap[string]interface{}`json:"status,omitempty"`}//varStockDataMapStock如果在控制台中打印,它看起来就像它应该的那样。我的Controller是这样的:packagelibimport("net/http""log""encoding/json")funcreturnStocksFromMemory

arrays - 函数应返回 sha256/sha384/sha512 结果作为 byte slice

我正在编写一个函数,它将输入数据作为字符串和要调用的SHA算法的位大小。它应该将生成的散列作为byteslice返回(第一次尝试):packagemainimport("crypto/sha256""crypto/sha512""errors""fmt")funcmain(){input:="Thisisatest."sha256,_:=shaSum(input,256)sha384,_:=shaSum(input,384)sha512,_:=shaSum(input,512)fmt.Println(input,sha256,sha384,sha512)}funcshaSum(data

go - 如何为以文件路径作为参数的golang方法提供Windows完整路径

所有golang方法都说ioutil.ReadFile理解unix路径,但它不采用windows路径。有没有办法以优雅的方式实现这一点,以便这些方法可以同时采用unix和windows路径。 最佳答案 您也可以在Windows中使用“/”。示例代码如下。packagemainimport("fmt""io/ioutil""log")funcmain(){content,err:=ioutil.ReadFile("D:/temp/main.go")iferr!=nil{log.Fatal(err)}fmt.Printf("Fileco

go - 不能在 db.Query 的参数中使用缓冲区(类型 bytes.Buffer)作为类型字符串

我在buffer.WriteString()中编写了一个SQL查询,但无法在db.Query()中使用该缓冲区。buffer.WriteString(fmt.Sprintf(`SELECTc.id,c.company_name,ss.start_date,ss.shift_length,ss.bill_rate,ss.ot_hrs,ss.dt_hrs,ts.pay_rate,ts.wc_rate,ts.paid,td.wcFROMcompanycJOINusersu1ONc.id=u1.company_idJOINschedulesONu1.id=s.user_idJOINschedu

time - 在 Go 中将格式化日期作为字符串返回

我想在go中以一种格式返回当前时间,我在时间格式方面没有问题,但是当在func中将它作为字符串返回时,我卡住了:packagemainimport("fmt""time")funcgetCurrentTime()string{t:=time.Now().Local()returnfmt.Sprintf("%s",t.Format("2006-01-0215:04:05+0800"))}funcmain(){fmt.Println("currentTimeis:",getCurrentTime)t:=time.Now().Local()fmt.Println("currentTimeis

go - 是否可以在 Go 中将结构作为参数传递?

我正在尝试将一组键、值传递给Go中的另一个函数。对Go很陌生,所以我正在努力弄清楚。packagemainimport("net/http""fmt""io/ioutil""net/url")typeParamsstruct{items[]KeyValue}typeKeyValuestruct{keystringvaluestring}funcmain(){data:=[]Params{KeyValue{key:"title",value:"Thingy"},KeyValue{key:"body",value:"Testing123"}}response,error:=makePost

无法读取 utmpx 文件

packagemainimport("os""fmt")funcmain(){fd,err:=os.Open("/var/run/utmpx")fmt.Println(fd,err)vardata[]bytelen,err:=fd.Read(data)fmt.Println(len,err)}&{0xc42000a240}nil0nil没有错误,也没有数据。这个路径/var/run/utmpx是从系统头文件中读取的。如何得到这个路径是anotherquestion系统:macOSelcapiton,go版本go1.8darwin/amd64**我的最终目标是将此文件读入gostruct

go - 如何调用父方法并将扩展父结构的任何子结构作为 Golang 中的参数传递

我还在学习Golang,想请教一下。是否有可能做这样的事情并将任何其他child传递给扩展父结构的PMethod?typeParentstruct{PAttributestring}func(p*Parent)PMethod(c*Child){fmt.Println("thisisparentAttribute:"+p.PAttribute)fmt.Println("thisischildAttribute:"+c.CAttribute)}typeChildstruct{ParentCAttributestring}typeChild2struct{ParentCAttributest

arrays - 不能在赋值 : need type assertion 中使用字(类型接口(interface) {})作为类型字符串

我是Go的新手,出于某种原因我正在做的事情对我来说似乎不是很直接。这是我的代码:for_,column:=rangeresp.Values{for_,word:=rangecolumn{s:=make([]string,1)s[0]=wordfmt.Print(s,"\n")}}我得到了错误:不能在赋值中使用word(typeinterface{})作为类型字符串:需要类型断言resp.Values是一个数组数组,所有数组都填充有字符串。reflect.TypeOf(resp.Values)返回[][]interface{},reflect.TypeOf(resp.Values[0])